Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @register_renderable macro. #306

Merged
merged 8 commits into from
Jun 19, 2019
Merged

Conversation

twavv
Copy link
Member

@twavv twavv commented Jun 7, 2019

This addresses some of the issues that currently exist around extending WebIO.

It allows for a library author to write

struct ScatterPlot
    x::Vector{Float64}
    y::Vector{Float64}
end

# All of the following are equivalent
# Option A
WebIO.@register_renderable function WebIO.render(x::ScatterPlot)
    # Construct the scatter plot using DOM primitives...
    return node(...)
end

# Option B
WebIO.@register_renderable WebIO.render(x::ScatterPlot) = node(...)

# Option C
function WebIO.render(x::ScatterPlot)
    ...
end
WebIO.@register_renderable ScatterPlot

This will automagically create the requisite Base.show methods for WEBIO_NODE_MIME and text/html which is required to work correctly in Jupyter.

Would help address JuliaPlots/PlotlyJS.jl#278.

@twavv twavv requested review from shashi and piever June 7, 2019 22:34
@codecov
Copy link

codecov bot commented Jun 7, 2019

Codecov Report

Merging #306 into master will increase coverage by 0.23%.
The diff coverage is 68.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #306      +/-   ##
==========================================
+ Coverage      59%   59.24%   +0.23%     
==========================================
  Files          17       17              
  Lines         622      633      +11     
==========================================
+ Hits          367      375       +8     
- Misses        255      258       +3
Impacted Files Coverage Δ
src/node.jl 83.05% <ø> (+3.94%) ⬆️
src/render.jl 25.67% <68.18%> (+12.94%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf5cac4...f81f3cd. Read the comment docs.

@piever
Copy link
Collaborator

piever commented Jun 8, 2019

I confess that I think there is some redundancy here... Once we have register_renderable, I imagine we don't really need the macro and register_renderable could also add a show method for WEBIO_NODE_MIME? Why defining one show method is a function and defining two is a macro?

As a counter-proposal, regardless of whether we choose function or macro (I personally prefer a macro not to have a @eval inside a function body), I would propose two methods:

macro register_renderable(T)
    # define show methods for T
end

and

macro register_renderable(f, T)
    # define show methods for T
    # define WebIO.render(x::T) = f(x)
end

To be used with the syntax:

@register_renderable(MyType) do val
    # define corresponding node
end

And we could probably deprecate register_renderable as a function.

@twavv
Copy link
Member Author

twavv commented Jun 8, 2019

Yes, the plan (after I realized that I duplicated some register renderable stuff) is definitely to deprecate the function version.

I also very much like that syntax (in addition to the bare @register_renderable MyType).

@twavv
Copy link
Member Author

twavv commented Jun 14, 2019

@shashi @piever Are y'all happy with this?

There's a bug(?) in Julia that makes the my preferred syntax slightly impossible (see JuliaLang/julia#32301).

# This doesn't work.
WebIO.@register_renderable(MyType) do x
    return node(:p, ...)
end

It does work with the uglier syntax where we put the @ before the WebIO. qualification:

# This works
@WebIO.register_renderable(MyType) do x ... end

It also works if you directly import the name.

# This works
using WebIO: @register_renderable
@register_renderable(MyType) do x ... end)

src/render.jl Outdated Show resolved Hide resolved
@twavv twavv merged commit 994db32 into master Jun 19, 2019
@twavv twavv deleted the td/add-register-renderable-macro branch December 22, 2019 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants